<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/favicon.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon.ico">
  <link rel="mask-icon" href="/favicon.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.loli.net/css?family=Lato:300,300italic,400,400italic,700,700italic%7CZCOOL+QingKe+HuangYou:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/blue/pace-theme-bounce.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"www.kaaaaai.cn","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.12.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="最近阅读 Swift 结构体何时使用 mutating 函数 2020 年疫情之下 iOS 面试题总结篇 UML类图几种关系的总结">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS - HMAC 加密算法和一个 MD5 加密的问题">
<meta property="og:url" content="https://www.kaaaaai.cn/ios-hmac-encryption-and-an-md5-problem.html">
<meta property="og:site_name" content="壹拾肆">
<meta property="og:description" content="最近阅读 Swift 结构体何时使用 mutating 函数 2020 年疫情之下 iOS 面试题总结篇 UML类图几种关系的总结">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-09-06T02:24:52.000Z">
<meta property="article:modified_time" content="2020-09-12T03:54:35.000Z">
<meta property="article:author" content="Kaaaaai">
<meta property="article:tag" content="EXP">
<meta property="article:tag" content="iOS">
<meta property="article:tag" content="OC">
<meta property="article:tag" content="Swift">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://www.kaaaaai.cn/ios-hmac-encryption-and-an-md5-problem.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://www.kaaaaai.cn/ios-hmac-encryption-and-an-md5-problem.html","path":"/ios-hmac-encryption-and-an-md5-problem.html","title":"iOS - HMAC 加密算法和一个 MD5 加密的问题"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>iOS - HMAC 加密算法和一个 MD5 加密的问题 | 壹拾肆</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-102185873-1"></script>
  <script class="next-config" data-name="google_analytics" type="application/json">{"tracking_id":"UA-102185873-1","only_pageview":false}</script>
  <script src="/js/third-party/analytics/google-analytics.js"></script>

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?9578d14210d67becef48447e4f6b3f20"></script>



<link rel="dns-prefetch" href="https://waline-server-kaaaaai.vercel.app"> <script type="text/javascript" src="/js/link-highlight.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="壹拾肆" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">壹拾肆</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">有些安排是多余的 这片空旷对我有益</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-moments"><a href="/bb/" rel="section"><i class="fa fa-coffee fa-fw"></i>碎片</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%80%E8%BF%91%E9%98%85%E8%AF%BB"><span class="nav-number">1.</span> <span class="nav-text">最近阅读</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">2.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#HMAC-SHA1-%E5%8A%A0%E5%AF%86"><span class="nav-number">3.</span> <span class="nav-text">HMAC SHA1 加密</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#MD5-%E5%8A%A0%E5%AF%86%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-number">4.</span> <span class="nav-text">MD5 加密的问题</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Kaaaaai"
      src="https://s2.loli.net/2022/03/29/GSJKrCRi4owa9hq.png">
  <p class="site-author-name" itemprop="name">Kaaaaai</p>
  <div class="site-description" itemprop="description">🐨🐨</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">100</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">66</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="sidebar-button site-overview-item animated">
    <button><i class="fa fa-comment"></i>
      Chat
    </button>
  </div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/Kaaaaai" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;Kaaaaai" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/users/8342030/kaaaaai" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;users&#x2F;8342030&#x2F;kaaaaai" rel="noopener" target="_blank"><i class="fab fa-stack-overflow fa-fw"></i>StackOverflow</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.instagram.com/edisonlllllk" title="Instagram → https:&#x2F;&#x2F;www.instagram.com&#x2F;edisonlllllk" rel="noopener" target="_blank"><i class="fab fa-instagram fa-fw"></i>Instagram</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:root@kaaaaai.cn" title="E-Mail → mailto:root@kaaaaai.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



        </div>
      </div>

      
    <!-- 网易云歌曲 -->
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=421203025&auto=1&height=66"></iframe>
        <div class="back-to-top animated" role="button" aria-label="返回顶部">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://www.kaaaaai.cn/ios-hmac-encryption-and-an-md5-problem.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://s2.loli.net/2022/03/29/GSJKrCRi4owa9hq.png">
      <meta itemprop="name" content="Kaaaaai">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="壹拾肆">
      <meta itemprop="description" content="🐨🐨">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="iOS - HMAC 加密算法和一个 MD5 加密的问题 | 壹拾肆">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          iOS - HMAC 加密算法和一个 MD5 加密的问题
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-06 10:24:52" itemprop="dateCreated datePublished" datetime="2020-09-06T10:24:52+08:00">2020-09-06</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">笔记</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
    <span class="post-meta-item-icon">
      <i class="far fa-comment"></i>
    </span>
    <span class="post-meta-item-text">Waline：</span>
  
    <a title="waline" href="/ios-hmac-encryption-and-an-md5-problem.html#waline" itemprop="discussionUrl">
      <span class="post-comments-count waline-comment-count" data-path="/ios-hmac-encryption-and-an-md5-problem.html" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5.1k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>5 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="最近阅读"><a href="#最近阅读" class="headerlink" title="最近阅读"></a>最近阅读</h2><ul>
<li><a target="_blank" rel="noopener" href="https://swift.gg/2016/06/17/when-to-use-mutating-functions-in-swift-structs/">Swift 结构体何时使用 mutating 函数</a></li>
<li><a target="_blank" rel="noopener" href="https://juejin.im/post/6862898534857834510">2020 年疫情之下 iOS 面试题总结篇</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/tianhai110/article/details/6339565">UML类图几种关系的总结</a></li>
</ul>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>最近我们部门重新定义了一下应用更新的接口，使用到的主要加密方式主要有 HMAC SHA1 加密和 MD5 加密。</p>
<h2 id="HMAC-SHA1-加密"><a href="#HMAC-SHA1-加密" class="headerlink" title="HMAC SHA1 加密"></a>HMAC SHA1 加密</h2><p>HMAC SHA1 加密的方法是在 Stack Overflow 上看到的 <a target="_blank" rel="noopener" href="https://stackoverflow.com/questions/24099520/commonhmac-in-swift/24411522#24411522">CommonHMAC in Swift</a></p>
<p>大概分两步：</p>
<ol>
<li>定义一个 HMAC 加密方式的枚举</li>
<li>实现一个 String 的拓展方法用来加密</li>
</ol>
<p>代码如下：</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> HMACAlgorithm &#123;</span><br><span class="line">    <span class="keyword">case</span> MD5, SHA1, SHA224, SHA256, SHA384, SHA512</span><br><span class="line"></span><br><span class="line">    func toCCHmacAlgorithm() -&gt; CCHmacAlgorithm &#123;</span><br><span class="line">        var result: Int = <span class="number">0</span></span><br><span class="line">        <span class="keyword">switch</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="keyword">case</span> .MD5:</span><br><span class="line">            result = kCCHmacAlgMD5</span><br><span class="line">        <span class="keyword">case</span> .SHA1:</span><br><span class="line">            result = kCCHmacAlgSHA1</span><br><span class="line">        <span class="keyword">case</span> .SHA224:</span><br><span class="line">            result = kCCHmacAlgSHA224</span><br><span class="line">        <span class="keyword">case</span> .SHA256:</span><br><span class="line">            result = kCCHmacAlgSHA256</span><br><span class="line">        <span class="keyword">case</span> .SHA384:</span><br><span class="line">            result = kCCHmacAlgSHA384</span><br><span class="line">        <span class="keyword">case</span> .SHA512:</span><br><span class="line">            result = kCCHmacAlgSHA512</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> CCHmacAlgorithm(result)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    func digestLength() -&gt; Int &#123;</span><br><span class="line">        var result: <span class="built_in">CInt</span> = <span class="number">0</span></span><br><span class="line">        <span class="keyword">switch</span> <span class="keyword">self</span> &#123;</span><br><span class="line">        <span class="keyword">case</span> .MD5:</span><br><span class="line">            result = CC_MD5_DIGEST_LENGTH</span><br><span class="line">        <span class="keyword">case</span> .SHA1:</span><br><span class="line">            result = CC_SHA1_DIGEST_LENGTH</span><br><span class="line">        <span class="keyword">case</span> .SHA224:</span><br><span class="line">            result = CC_SHA224_DIGEST_LENGTH</span><br><span class="line">        <span class="keyword">case</span> .SHA256:</span><br><span class="line">            result = CC_SHA256_DIGEST_LENGTH</span><br><span class="line">        <span class="keyword">case</span> .SHA384:</span><br><span class="line">            result = CC_SHA384_DIGEST_LENGTH</span><br><span class="line">        <span class="keyword">case</span> .SHA512:</span><br><span class="line">            result = CC_SHA512_DIGEST_LENGTH</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> Int(result)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">extension String &#123;</span><br><span class="line">    func hmac(algorithm: HMACAlgorithm, key: String) -&gt; String &#123;</span><br><span class="line">        let cKey = key.cStringUsingEncoding(<span class="built_in">NSUTF8StringEncoding</span>)</span><br><span class="line">        let cData = <span class="keyword">self</span>.cStringUsingEncoding(<span class="built_in">NSUTF8StringEncoding</span>)</span><br><span class="line">        var result = [CUnsignedChar](count: Int(algorithm.digestLength()), repeatedValue: <span class="number">0</span>)</span><br><span class="line">        CCHmac(algorithm.toCCHmacAlgorithm(), cKey!, strlen(cKey!), cData!, strlen(cData!), &amp;result)</span><br><span class="line">        var hmacData:<span class="built_in">NSData</span> = <span class="built_in">NSData</span>(bytes: result, length: (Int(algorithm.digestLength())))</span><br><span class="line">        var hmacBase64 = hmacData.base64EncodedStringWithOptions(<span class="built_in">NSDataBase64EncodingOptions</span>.Encoding76CharacterLineLength)</span><br><span class="line">        <span class="keyword">return</span> String(hmacBase64)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="MD5-加密的问题"><a href="#MD5-加密的问题" class="headerlink" title="MD5 加密的问题"></a>MD5 加密的问题</h2><p>细心的朋友注意到，上面 HMAC 加密的枚举里有一个 MD5 的字段，如果同时用到  HMAC 加密和 MD5 加密的朋友，需要注意一下 Hmac-MD5 加密和 MD5 加密的区别，推荐可以用在线加密工具比较直观的看到加密结果的区别：<a target="_blank" rel="noopener" href="https://tool.oschina.net/encrypt?type=2">在线加解密</a>。另外这个工具也可以用于通信双方用来和自己解密的结果校验是否正确。</p>
<p>回归正题，<code>MD5</code> 加密本身没什么难度，网上搜索一大把。抱着做笔记的目的贴一下 Swift 的源码：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">///字符直接转 md5</span></span><br><span class="line"><span class="keyword">func</span> <span class="title function_">conversionToMD5</span>() -&gt; <span class="type">String</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> str <span class="operator">=</span> <span class="keyword">self</span>.cString(using: <span class="type">String</span>.<span class="type">Encoding</span>.utf8)</span><br><span class="line">    <span class="keyword">let</span> strLen <span class="operator">=</span> <span class="type">CUnsignedInt</span>(<span class="keyword">self</span>.lengthOfBytes(using: <span class="type">String</span>.<span class="type">Encoding</span>.utf8))</span><br><span class="line">    <span class="keyword">let</span> digestLen <span class="operator">=</span> <span class="type">Int</span>(<span class="type">CC_MD5_DIGEST_LENGTH</span>)</span><br><span class="line">    <span class="keyword">let</span> result <span class="operator">=</span> <span class="type">UnsafeMutablePointer</span>&lt;<span class="type">UInt8</span>&gt;.allocate(capacity: <span class="number">16</span>)</span><br><span class="line">    <span class="type">CC_MD5</span>(str<span class="operator">!</span>, strLen, result)</span><br><span class="line">    <span class="keyword">let</span> hash <span class="operator">=</span> <span class="type">NSMutableString</span>()</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="number">0</span> <span class="operator">..&lt;</span> digestLen &#123;</span><br><span class="line">        hash.appendFormat(<span class="string">&quot;%02x&quot;</span>, result[i])</span><br><span class="line">    &#125;</span><br><span class="line">    free(result)</span><br><span class="line">    <span class="keyword">return</span> <span class="type">String</span>(format: hash <span class="keyword">as</span> <span class="type">String</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后问题就来了：iOS 端的加密和服务器那边的加密结果无法对应，导致鉴权失败。<br>排查问题，让服务器同事打印在 <code>MD5</code> 加密前的每个步骤的值，发现都是一致的，在 <code>MD5</code> 加密后就不一致了。然后去就看了一下他写的源码，服务器那边的逻辑使用 <code>Java</code> 写的，发现他丢进 <code>MD5</code> 加密时是把上一步的结果调用 <code>getBytes()</code> 语句后丢进去的。即<strong>加密的是字符串转换后的字节数组</strong>。所以在 iOS 端这边的处理也需要将 <code>String</code> 转换成 <code>Data</code> 数据来加密。</p>
<p>所以步骤分为以下两步：<br>1.把 <code>String</code> 类型数据转换成 16 进制 <code>Data</code> 数据——注意是 16 进制的 <code>Data</code> 数据，如果直接用原生方法 <code>&quot;&lt;#String#&gt;&quot;.(using: .utf8)</code> 转换肯定是不行的。<br>2.把 <code>Data</code> 数据丢进 <code>MD5</code> 方法中进行加密<br>而在网上搜了一圈，Swift 版本大部分看到的都是用字符串进行 <code>MD5</code> 加密，最后仍是在 Stack Overflow 上看到了一个正确的结果 - <a target="_blank" rel="noopener" href="https://stackoverflow.com/questions/39400495/md5-of-data-in-swift-3">MD5 of Data in Swift 3</a></p>
<p>最后和 <a target="_blank" rel="noopener" href="https://www.jianshu.com/p/95dd32a15246"><code>String</code> 转 <code>Data</code> 的方法</a>整合以后。代码如下：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">///转换成 Data 数据再转换成 md5</span></span><br><span class="line"><span class="keyword">func</span> <span class="title function_">conversionToDataToMD5</span>() -&gt; <span class="type">String</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> data <span class="operator">=</span> <span class="type">Data</span>(hex: <span class="keyword">self</span>)</span><br><span class="line">    <span class="keyword">var</span> digest <span class="operator">=</span> [<span class="type">UInt8</span>](repeating: <span class="number">0</span>, count: <span class="type">Int</span>(<span class="type">CC_MD5_DIGEST_LENGTH</span>))</span><br><span class="line"></span><br><span class="line">    data.withUnsafeBytes &#123;</span><br><span class="line">        <span class="type">CC_MD5</span>(<span class="variable">$0</span>.baseAddress, <span class="type">UInt32</span>(data.count), <span class="operator">&amp;</span>digest)</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">var</span> hashString <span class="operator">=</span> <span class="string">&quot;&quot;</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span> byte <span class="keyword">in</span> digest &#123;</span><br><span class="line">        hashString <span class="operator">+=</span> <span class="type">String</span>(format:<span class="string">&quot;%02x&quot;</span>, <span class="type">UInt8</span>(byte))</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> hashString</span><br><span class="line">&#125;</span><br><span class="line">    </span><br><span class="line"><span class="keyword">extension</span> <span class="title class_">Data</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">init</span>(<span class="params">hex</span>: <span class="type">String</span>) &#123;</span><br><span class="line">        <span class="keyword">self</span>.<span class="keyword">init</span>(bytes: <span class="type">Array</span>&lt;<span class="type">UInt8</span>&gt;(hex: hex))</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">var</span> bytes: <span class="type">Array</span>&lt;<span class="type">UInt8</span>&gt; &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="type">Array</span>(<span class="keyword">self</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">extension</span> <span class="title class_">Array</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">init</span>(<span class="params">reserveCapacity</span>: <span class="type">Int</span>) &#123;</span><br><span class="line">        <span class="keyword">self</span> <span class="operator">=</span> <span class="type">Array</span>&lt;<span class="type">Element</span>&gt;()</span><br><span class="line">        <span class="keyword">self</span>.reserveCapacity(reserveCapacity)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">extension</span> <span class="title class_">Array</span> <span class="keyword">where</span> <span class="type">Element</span> <span class="operator">==</span> <span class="type">UInt8</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">init</span>(<span class="params">hex</span>: <span class="type">String</span>) &#123;</span><br><span class="line">        <span class="keyword">self</span>.<span class="keyword">init</span>(reserveCapacity: hex.unicodeScalars.lazy.underestimatedCount)</span><br><span class="line">        <span class="keyword">var</span> buffer: <span class="type">UInt8</span>?</span><br><span class="line">        <span class="keyword">var</span> skip <span class="operator">=</span> hex.hasPrefix(<span class="string">&quot;0x&quot;</span>) <span class="operator">?</span> <span class="number">2</span> : <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> char <span class="keyword">in</span> hex.unicodeScalars.lazy &#123;</span><br><span class="line">            <span class="keyword">guard</span> skip <span class="operator">==</span> <span class="number">0</span> <span class="keyword">else</span> &#123;</span><br><span class="line">                skip <span class="operator">-=</span> <span class="number">1</span></span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">guard</span> char.value <span class="operator">&gt;=</span> <span class="number">48</span> <span class="operator">&amp;&amp;</span> char.value <span class="operator">&lt;=</span> <span class="number">102</span> <span class="keyword">else</span> &#123;</span><br><span class="line">                removeAll()</span><br><span class="line">                <span class="keyword">return</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">let</span> v: <span class="type">UInt8</span></span><br><span class="line">            <span class="keyword">let</span> c: <span class="type">UInt8</span> <span class="operator">=</span> <span class="type">UInt8</span>(char.value)</span><br><span class="line">            <span class="keyword">switch</span> c &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="keyword">let</span> c <span class="keyword">where</span> c <span class="operator">&lt;=</span> <span class="number">57</span>:</span><br><span class="line">                v <span class="operator">=</span> c <span class="operator">-</span> <span class="number">48</span></span><br><span class="line">            <span class="keyword">case</span> <span class="keyword">let</span> c <span class="keyword">where</span> c <span class="operator">&gt;=</span> <span class="number">65</span> <span class="operator">&amp;&amp;</span> c <span class="operator">&lt;=</span> <span class="number">70</span>:</span><br><span class="line">                v <span class="operator">=</span> c <span class="operator">-</span> <span class="number">55</span></span><br><span class="line">            <span class="keyword">case</span> <span class="keyword">let</span> c <span class="keyword">where</span> c <span class="operator">&gt;=</span> <span class="number">97</span>:</span><br><span class="line">                v <span class="operator">=</span> c <span class="operator">-</span> <span class="number">87</span></span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                removeAll()</span><br><span class="line">                <span class="keyword">return</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> <span class="keyword">let</span> b <span class="operator">=</span> buffer &#123;</span><br><span class="line">                append(b <span class="operator">&lt;&lt;</span> <span class="number">4</span> <span class="operator">|</span> v)</span><br><span class="line">                buffer <span class="operator">=</span> <span class="literal">nil</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                buffer <span class="operator">=</span> v</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">let</span> b <span class="operator">=</span> buffer &#123;</span><br><span class="line">            append(b)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>使用：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> md5Str <span class="operator">=</span> <span class="string">&quot;两个黄鹂鸣翠柳，一行白鹭上青天。&quot;</span>.conversionToDataToMD5()</span><br></pre></td></tr></table></figure>

<p>然后因为业务原因，也用 OC 实现了这一整套的加密流程，最后一步的转换加密也大同小异：</p>
<ol>
<li>转换成 16 进制 <code>NSData</code> 数据<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">+(NSData*)getHexDataWithString:(NSString*)string&#123;</span><br><span class="line">    const char *buf = [string UTF8String];</span><br><span class="line">    </span><br><span class="line">    NSMutableData *data = [NSMutableData data];</span><br><span class="line">    if (buf)&#123;</span><br><span class="line">        uint32_t len = strlen(buf);</span><br><span class="line">        char singleNumberString[3] = &#123;&#x27;\0&#x27;, &#x27;\0&#x27;, &#x27;\0&#x27;&#125;;</span><br><span class="line">        uint32_t singleNumber = 0;</span><br><span class="line">        for(uint32_t i = 0 ; i &lt; len; i+=2)&#123;</span><br><span class="line">            if ( ((i+1) &lt; len) &amp;&amp; isxdigit(buf[i]) &amp;&amp; (isxdigit(buf[i+1])) )&#123;</span><br><span class="line"></span><br><span class="line">                 singleNumberString[0] = buf[i];</span><br><span class="line"></span><br><span class="line">                 singleNumberString[1] = buf[i + 1];</span><br><span class="line"></span><br><span class="line">                 sscanf(singleNumberString, &quot;%x&quot;, &amp;singleNumber);</span><br><span class="line"></span><br><span class="line">                 uint8_t tmp = (uint8_t)(singleNumber &amp; 0x000000FF);</span><br><span class="line"></span><br><span class="line">                 [data appendBytes:(void *)(&amp;tmp)length:1];</span><br><span class="line">            &#125;else&#123;</span><br><span class="line">                break;</span><br><span class="line">            &#125;</span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line">    return data;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><code>NSData</code> <code>MD5</code> 加密<br>OC<code>MD5</code> 的加密就概不累述了，OC 比 Swift 更容易搜到，放一个我参考的博客：<a target="_blank" rel="noopener" href="https://blog.csdn.net/u011349387/article/details/47318679">Objective-C NSString NSData Byte 等转换</a></li>
</ol>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div></div>
  <button>
    请喝咖啡
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/loading-style-3.gif" data-original="https://s2.loli.net/2022/07/02/Gce8a5QAqrZ3X9R.gif" alt="Kaaaaai 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Kaaaaai
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://www.kaaaaai.cn/ios-hmac-encryption-and-an-md5-problem.html" title="iOS - HMAC 加密算法和一个 MD5 加密的问题">https://www.kaaaaai.cn/ios-hmac-encryption-and-an-md5-problem.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/EXP/" rel="tag"><i class="fa fa-tag"></i> EXP</a>
              <a href="/tags/iOS/" rel="tag"><i class="fa fa-tag"></i> iOS</a>
              <a href="/tags/OC/" rel="tag"><i class="fa fa-tag"></i> OC</a>
              <a href="/tags/Swift/" rel="tag"><i class="fa fa-tag"></i> Swift</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/ios-some-question-about-bluetooth.html" rel="prev" title="iOS - 一些蓝牙问题的解决">
                  <i class="fa fa-chevron-left"></i> iOS - 一些蓝牙问题的解决
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/swift-permission-request-tools.html" rel="next" title="Swift - 权限请求封装">
                  Swift - 权限请求封装 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="waline"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Kaaaaai</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">234k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:33</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <!-- time -->
  <div id="show-time">
    <span id="span_dt_dt"></span>
  </div>
  <script>
      function show_date_time() {
        window.setTimeout("show_date_time()", 1000);
        BirthDay = new Date("6/23/2017 19:37:01");
        today = new Date();
        //总时间
        timeold = (today.getTime() - BirthDay.getTime());
        sectimeold = timeold / 1000
        secondsold = Math.floor(sectimeold);
        msPerDay = 24 * 60 * 60 * 1000
        e_daysold = timeold / msPerDay
        daysold = Math.floor(e_daysold);
        e_hrsold = (e_daysold - daysold) * 24;
        hrsold = Math.floor(e_hrsold);
        e_minsold = (e_hrsold - hrsold) * 60;
        minsold = Math.floor((e_hrsold - hrsold) * 60);
        seconds = Math.floor((e_minsold - minsold) * 60);
        span_dt_dt.innerHTML = "已存活 " + daysold + " 天 " + hrsold + " 小时 " + minsold + " 分 " + seconds + " 秒";
    }
    show_date_time();
  </script>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.5.0/pjax.min.js" integrity="sha256-3NkoLDrmHLTYj7csHIZSr0MHAFTXth7Ua/DDt4MRUAg=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>
<script class="next-config" data-name="chatra" type="application/json">{"enable":true,"async":true,"id":"QXLRxXcsfrRJR2KTG"}</script>
<script src="/js/third-party/chat/chatra.js"></script>
<script async src="https://call.chatra.io/chatra.js"></script>



  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>

  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




<script class="next-config" data-name="waline" type="application/json">{"lang":"zh-cn","enable":true,"serverURL":"https://waline-server-kaaaaai.vercel.app","cssUrl":"https://unpkg.com/@waline/client@v2/dist/waline.css","commentCount":true,"pageview":false,"placeholder":"请文明评论呀","avatar":"mm","meta":["nick","mail","link"],"pageSize":10,"visitor":false,"comment_count":true,"requiredFields":[],"libUrl":"//unpkg.com/@waline/client@v2/dist/waline.js","el":"#waline","comment":true,"path":"/ios-hmac-encryption-and-an-md5-problem.html"}</script>
<link rel="stylesheet" href="https://unpkg.com/@waline/client@v2/dist/waline.css">
<script>
document.addEventListener('page:loaded', () => {
  NexT.utils.loadComments(CONFIG.waline.el).then(() =>
    NexT.utils.getScript(CONFIG.waline.libUrl, { condition: window.Waline })
  ).then(() => 
    Waline.init(Object.assign({}, CONFIG.waline,{ el: document.querySelector(CONFIG.waline.el) }))
  );
});
</script>


        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: true,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(r){r.imageLazyLoadSetting.processImages=t;var a=r.imageLazyLoadSetting.isSPA,n=r.imageLazyLoadSetting.preloadRatio||1,d=o();function o(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(t){(a||t)&&(d=o());for(var e,i=0;i<d.length;i++)0<=(e=(e=d[i]).getBoundingClientRect()).bottom&&0<=e.left&&e.top<=(r.innerHeight*n||document.documentElement.clientHeight*n)&&function(){var t,e,a,n,o=d[i];e=function(){d=d.filter(function(t){return o!==t}),r.imageLazyLoadSetting.onImageLoaded&&r.imageLazyLoadSetting.onImageLoaded(o)},(t=o).dataset.loaded||(t.hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(a=new Image,n=t.getAttribute("data-original"),a.onload=function(){t.src=n,t.removeAttribute("data-original"),t.setAttribute("data-loaded",!0),e&&e()},a.onerror=function(){t.removeAttribute("data-original"),t.setAttribute("data-loaded",!1),t.src=n},t.src!==n&&(a.src=n)))}()}function e(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",e),r.addEventListener("resize",e),r.addEventListener("orientationchange",e)}(this);</script></body>
</html>
